package com.financing.platform.web.api;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.platform.common.Pager;
import com.financing.platform.common.ResultCode;
import com.financing.platform.common.ResultMap;
import com.financing.platform.common.ResultMap.STATUS;
import com.financing.platform.dto.LicaishiManagerUserDTO;
import com.financing.platform.dto.UserInfoDTO;
import com.financing.platform.service.IFaUserService;
import com.financing.platform.service.ILicaishiManagerUserService;
import com.financing.platform.util.DTOdateSet;
import com.financing.platform.vo.LicaishiManagerUserVO;

@Controller("licaishiManagerUserApi")
@RequestMapping("/api/licaishi")
@Scope("prototype")
public class LicaishiManagerUserAction extends BaseAction {

	
	@Autowired
	IFaUserService faUserServiceImpl;
	@Autowired
	private ILicaishiManagerUserService licaishiManagerUserServiceImpl;

	@RequestMapping("/queryNotLicaishiManagerUserList.shtml")
	@ResponseBody
	public ResultMap queryNotLicaishiManagerUser(HttpServletRequest request, HttpServletResponse response, LicaishiManagerUserVO licaishiManagerVo, Pager page) {
		try {
			int count = licaishiManagerUserServiceImpl.queryNotLicaishiManagerUsersCount(licaishiManagerVo);
			List<LicaishiManagerUserVO> list = null;
			if (count > 0) {
				list = licaishiManagerUserServiceImpl.queryNotLicaishiManagerUsers(licaishiManagerVo);
			}
			resultMap.setAttribute("list", list);
			resultMap.setAttribute("page", page);
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
		} catch (SQLException e) {
			log.error("queryNotLicaishiManagerUser error", e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}

		return resultMap;
	}

	@RequestMapping("/queryLicaishiManagerUsersList.shtml")
	@ResponseBody
	public ResultMap queryLicaishiManagerUsers(HttpServletRequest request, HttpServletResponse response, LicaishiManagerUserVO user, Pager page) {
		try {
			int count = licaishiManagerUserServiceImpl.queryLicaishiManagerUsersCount(user);
			page.setTotalCount(count);
			user.setBegin(page.getStartIndex());
			user.setEnd(page.getPageSize());
			List<LicaishiManagerUserVO> list = null;
			List<LicaishiManagerUserVO> treeList  =  null;
			if (count > 0) {

				list = licaishiManagerUserServiceImpl.queryLicaishiManagerUsers(user);

				treeList = new ArrayList<LicaishiManagerUserVO>();

				setTreeList(treeList,null,list,true);
			}


			//resultMap.setAttribute("listTree", treeList);
			resultMap.setAttribute("list", treeList);
			resultMap.setAttribute("page", page);
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
		} catch (SQLException e) {
			log.error("queryLicaishiManagerUsers error", e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		return resultMap;
	}
	private  void setTreeList(List<LicaishiManagerUserVO> treeList,LicaishiManagerUserVO treeBean ,List<LicaishiManagerUserVO> list,boolean flag){

		for(LicaishiManagerUserVO bean:list){
			if(flag){
				if(StringUtils.isBlank(bean.getParentUserId())){
					treeList.add(bean);
					setTreeList(treeList,bean,list,false);
				}
			}else{
				if(treeBean.getUserId().equals(bean.getParentUserId())){
					String userNameFlag = "";
					for (int i = 1; i < bean.getLayerPath().split(",").length; i++) {
						userNameFlag = "..."+userNameFlag;
					}
					bean.setUsername(userNameFlag+bean.getUsername());
					treeList.add(bean);
					setTreeList(treeList,bean,list,false);
				}
			}
		}
	}

	@RequestMapping("/queryLicaishiManagerUsersListForAdviser.shtml")
	@ResponseBody
	public ResultMap queryLicaishiManagerUsersListForAdviser(HttpServletRequest request, HttpServletResponse response, LicaishiManagerUserVO user, Pager page) {
		try {
			HttpSession session = request.getSession();
			UserInfoDTO userInfoDTO = (UserInfoDTO) session.getAttribute(DTOdateSet.user);
			
			int count = licaishiManagerUserServiceImpl.queryLicaishiManagerUsersCount(user);
			page.setTotalCount(count);
			user.setBegin(page.getStartIndex());
			user.setEnd(page.getPageSize());
			List<LicaishiManagerUserVO> list = null;
			if (count > 0) {
				list = licaishiManagerUserServiceImpl.queryLicaishiManagerUsers(user);
			}
			
			
			List<String> systemUserIds = faUserServiceImpl.queryFaUserSubUserId(userInfoDTO.getUserId());
			systemUserIds.add(userInfoDTO.getUserId());
			List<LicaishiManagerUserVO> listForAdviser = new ArrayList<LicaishiManagerUserVO>();
			for (LicaishiManagerUserVO licaishiManagerUserVO : list) {
				String userId = licaishiManagerUserVO.getUserId();
				if(systemUserIds.contains(userId)){
					listForAdviser.add(licaishiManagerUserVO);
				}
			}
			
			resultMap.setAttribute("list", listForAdviser);
			resultMap.setAttribute("page", page);
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
		} catch (SQLException e) {
			log.error("queryLicaishiManagerUsers error", e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		return resultMap;
	}
	
	@RequestMapping("/licaishiManagerUserAdd.shtml")
	@ResponseBody
	public ResultMap licaishiManagerUserAdd(HttpServletRequest request, HttpServletResponse response, LicaishiManagerUserDTO user){
		try {
			if(user != null && user.getSystemUserId() != null && !"".equals(user.getSystemUserId())){
				DTOdateSet.setCreateInfo(user, request.getSession());
				DTOdateSet.setUpdateInfo(user, request.getSession());
				LicaishiManagerUserVO tmpUser = new LicaishiManagerUserVO();
				tmpUser.setSystemUserId(user.getSystemUserId());
				
				List<LicaishiManagerUserVO> list = licaishiManagerUserServiceImpl.queryLicaishiManagerUsers(tmpUser);
				if (list != null && list.size() > 0) {
					resultMap.setStatus(STATUS.FAILE);
					resultMap.setStatusCode(ResultCode.STATUS_CODE_REPEAT_LOSE);
				}else {
					licaishiManagerUserServiceImpl.addLicaishiManagerUser(user);
					
					resultMap.setStatus(STATUS.SUCCESS);
					resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
				}
				
			}else {
				resultMap.setStatus(STATUS.FAILE);
				resultMap.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
			}
		} catch (Exception e) {
			log.error("LicaishiManagerUserAdd error", e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return resultMap;
	}
}
